<?php
/**
* User Coupons Controller
*
* @access 	public
* @author 	Valentin
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/api
* @since  	06-2010
*/
class RM_User_CouponsController extends RM_Controller
{
    /**
     * This method will apply coupon code to selected unit
     *
     * @return array
     */
    public function applyJsonAction(){
        $translate = RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS);

        $code = $this->getRequest()->getParam('code');
        if ($code == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'CodeEmpty')
            ));
        }

        $unitID = $this->getRequest()->getParam('unitID');
        if ($unitID == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'UnitEmpty')
            ));
        }

        $unitModel = new RM_Units();
        $unit = $unitModel->find($unitID)->current();
        if ($unit == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'WrongUnit')
            ));
        }

        $couponsModel = new RM_Coupons();
        $coupon = $couponsModel->getBy($code, $unit);
        if ($coupon == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'WrongCode')
            ));
        }

        $user = RM_Reservation_Manager::getInstance()->getUser();
        if ($user !== null && $coupon->isUsedBy($user)) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'Used')
            ));
        }

        $manager = new RM_Plugin_Coupons();
        $alreadySelected = $manager->getSelected($unit);
        foreach ($alreadySelected as $alreadySelectedCoupon) {
            if ($alreadySelectedCoupon == $coupon->id) {
                return array('data' => array(
                    'success' => false,
                    'error' => $translate->_('User.Coupons.Errors', 'Using')
                ));
            }
        }

        //We also need to check if this coupon is matched with reservation details or not
        $detail = RM_Reservation_Manager::getInstance()->getDetails($unit);
        if ($coupon->isMatched($detail) == false) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'InvalidCode')
            ));
        }

        $manager->saveSelected($unit, $coupon);
        return array('data' => array('success' => true));
    }

    public function deleteJsonAction(){
        $translate = RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS);

        $code = $this->getRequest()->getParam('code');
        if ($code == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'CodeEmpty')
            ));
        }

        $unitID = $this->getRequest()->getParam('unitID');
        if ($unitID == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'UnitEmpty')
            ));
        }

        $unitModel = new RM_Units();
        $unit = $unitModel->find($unitID)->current();
        if ($unit == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'WrongUnit')
            ));
        }

        $couponsModel = new RM_Coupons();
        $coupon = $couponsModel->getBy($code, $unit);
        if ($coupon == null) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'WrongCode')
            ));
        }

        $manager = new RM_Plugin_Coupons();
        $result = $manager->deleteSelected($unit, $coupon);
        if ($result == false) {
            return array('data' => array(
                'success' => false,
                'error' => $translate->_('User.Coupons.Errors', 'NoCode')
            ));
        }

        return array('data' => array('success' => true));        
    }
}